package cn.hengzhu.main_manager.utils;

import java.util.Date;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayOpenAppMiniTemplatemessageSendRequest;
import com.alipay.api.response.AlipayOpenAppMiniTemplatemessageSendResponse;

import cn.hengzhu.main_manager.repository.param.AliSendMessageDate;
import cn.hengzhu.main_manager.repository.param.AliXcxSendMessageParam;
import cn.hengzhu.main_manager.repository.param.SendMessageContent;
import cn.hengzhu.main_manager.repository.param.SendMessageDate;
import cn.hengzhu.main_manager.repository.param.WXXcxSendMessageParam;
import cn.hengzhu.main_manager.utils.config.xcx.AliConfig;
import cn.hengzhu.main_manager.utils.config.xcx.WxConfig;

/**
 * @author liuCheng
 * @since 2019年1月7日 下午5:54:34 
 * 微信，支付宝小程序的消息发送
 */
public class SendXcxMessageUtil {


	public static Integer sendWxMessage(String name,Date saveTime,Date endTime,String access_token,String prepayId,String openId,String page) {
		
		String saveTimeStr = DateTimeHelper.formatDateTimetoString(saveTime, "yyyy-MM-dd HH:mm");
		String endTimeStr = DateTimeHelper.formatDateTimetoString(endTime, "yyyy-MM-dd HH:mm");
		String sendUrl = WxConfig.SEND_URL.replace("ACCESS_TOKEN", access_token);
		WXXcxSendMessageParam param = new WXXcxSendMessageParam();
		SendMessageDate date = new SendMessageDate();
		SendMessageContent content1 = new SendMessageContent();
		SendMessageContent content2 = new SendMessageContent();
		SendMessageContent content3 = new SendMessageContent();
		content1.setValue(name);
		date.setKeyword1(content1);
		content2.setValue(saveTimeStr);
		date.setKeyword2(content2);
		content3.setValue(endTimeStr+"(超出此时间，会被自动清理)");
		date.setKeyword3(content3);
		
		param.setTouser(openId);
		param.setForm_id(prepayId);
		param.setTemplate_id(WxConfig.TEMPLATE_ID);
		param.setData(date);
		param.setPage(page);
		String jsonString = JSONObject.toJSONString(param);
		String httpPost = HttpClientUtil.httpPost(sendUrl, jsonString);
		JSONObject parseObject = JSONObject.parseObject(httpPost);
		Integer errcode = (Integer)parseObject.get("errcode");
		return errcode;
	}
	
	
	public static void sendMessageToAli(String form_id,String to_user_id,Date saveTime,String name,Date endDate,String page) throws Exception{
		String dateStr = DateTimeHelper.formatDateTimetoString(saveTime, "yyyy-MM-dd HH:mm");
		String endDateStr = DateTimeHelper.formatDateTimetoString(endDate, "yyyy-MM-dd HH:mm");
		AliXcxSendMessageParam aliXcxSendMessageParam=new AliXcxSendMessageParam();
		AliSendMessageDate aliSendMessageDate=new AliSendMessageDate();
		SendMessageContent sendMessageContent1=new SendMessageContent();
		SendMessageContent sendMessageContent2=new SendMessageContent();
		SendMessageContent sendMessageContent3=new SendMessageContent();
		sendMessageContent1.setValue(name);
		sendMessageContent2.setValue(dateStr);
		sendMessageContent3.setValue(endDateStr+"(超出此时间，会被自动清理)");
		aliSendMessageDate.setKeyword1(sendMessageContent1);
		aliSendMessageDate.setKeyword2(sendMessageContent2);
		aliSendMessageDate.setKeyword3(sendMessageContent3);
		
		aliXcxSendMessageParam.setForm_id(form_id);
		aliXcxSendMessageParam.setTo_user_id(to_user_id);
		aliXcxSendMessageParam.setUser_template_id(AliConfig.TEMPLATE_ID);
		aliXcxSendMessageParam.setData(aliSendMessageDate);
		aliXcxSendMessageParam.setPage(page);
		
		
		String param = JSONObject.toJSONString(aliXcxSendMessageParam);
		
		AlipayClient alipayClient = new DefaultAlipayClient(AliConfig.URL, AliConfig.APP_ID, AliConfig.PRIVATE_KEY,
				"json", "GBK", AliConfig.ALIPAY_PUBLIC_KEY, "RSA2");
		AlipayOpenAppMiniTemplatemessageSendRequest request = new AlipayOpenAppMiniTemplatemessageSendRequest();
		request.setBizContent(param);
		AlipayOpenAppMiniTemplatemessageSendResponse response = alipayClient.execute(request);
		if (response.isSuccess()) {
//			System.out.println("调用成功");
		} else {
//			System.out.println("调用失败");
		}
	}
	
	
	

}


